package com.controller;
import com.mapper.StudentMapper;

import com.mapper.UserMapper;
import com.pojo.Admin;
import com.pojo.SClass;
import com.pojo.Student;
import com.pojo.Teacher;
import com.utils.Tool;
import com.utils.WxTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 获取 open_id
     * @param code code
     */
    @RequestMapping("/getOpenId")
    public Object getOpenId(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session" +
                "?appid=wxd5bf0c94aed3f0db" +
                "&secret=a9b8d6133dd5e811ced3502219754f46" +
                "&grant_type=authorization_code" +
                "&js_code=" + code;

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new WxTool());

        return restTemplate.getForObject(url, Object.class);
    }

    /**
     * 获取用户信息
     * role 0:admin 1:teacher 2:student
     * @param openid 小程序 open_id
     */
    @RequestMapping("/getInfo")
    public Map<String,Object> getInfo(String openid,Integer role) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";
        Admin admin=new Admin();
        Teacher teacher=new Teacher();
        Student student=new Student();
        if (openid == null || role==null) {
            code = 500;
            msg = "open_id与role不能为空";
        } else {
            switch (role){
                case 0: admin=userMapper.getAdminByOpenId(openid);
                        if(admin==null){
                            code=501;
                            msg="管理员不存在!";
                        }else{
                            userMapper.upAdminLoginTime(admin.getId());//设置登陆时间
                            admin.setPassword(null);
                            map.put("data",admin);
                        }
                        break;
                case 1: teacher=userMapper.getTeacherByOpenId(openid);
                        if(teacher==null){
                            code=501;
                            msg="教师不存在!";
                        }else{
                            userMapper.upAdminLoginTime(teacher.getId());
                            teacher.setPassword(null);
                            teacher.setStation_name(userMapper.getStationNameById(teacher.getStation_id()));
                            map.put("data",teacher);
                        }
                        break;
                case 2: student = studentMapper.getInfo(openid);
                        if(student==null) {
                            code = 501;
                            msg = "学生不存在";
                        } else {
                            SClass sClass = studentMapper.getClass(student.getClass_id());
                            student.setSdept_name(studentMapper.getSdeptName(sClass.getSdept_id()));
                            student.setClass_name(studentMapper.getClassName(student.getClass_id()));
                            student.setPassword(null);
                            map.put("data",student);
                        }
                        break;
                default: code=500; msg="角色码有误!"; break;
            }
        }
        map.put("code",code);
        map.put("msg",msg);
        return map;
    }
}
